package org.example;

import java.util.concurrent.CountDownLatch;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        //初始化10，表示有10个任务需要完成
        CountDownLatch countDownLatch=new CountDownLatch(10);
        Runnable r=new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep((long) (Math.random() * 10000));
                    //内部计数器自减
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 10; i++){
            new Thread(r).start();
        }
        //阻塞等待所有任务执行完毕
        //相当于计数器为0
        countDownLatch.await();
        System.out.println("比赛结束");
    }
}
